package com.taozi.netty.socketio.use.classroomchat.service.socketio;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import com.taozi.netty.socketio.use.classroomchat.constant.EducationConstant;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomUser;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomChatMapper;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomUserMapper;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import com.taozi.netty.socketio.use.classroomchat.utils.CpssStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description 课堂聊天人员列表监听
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Service("chatUserListener")
@Slf4j
public class ChatUserListener implements DataListener<ClassroomChatVo> {

    @Autowired
    ClassroomChatMapper classroomChatMapper;
    @Autowired
    ClassroomUserMapper classroomUserMapper;

    SocketIOServer socketIOServer;
    public void setServer(SocketIOServer server) {
        this.socketIOServer = server;
    }

    @Override
    public void onData(SocketIOClient socketIOClient, ClassroomChatVo classroomChatVo, AckRequest ackRequest) throws Exception {

        log.info("chatUser  " + classroomChatVo.toString());
        if(classroomChatVo == null || classroomChatVo.getClassroomId() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }

        //根据课堂id查询课堂在线人员
        classroomChatVo.setIsOnline(EducationConstant.STATUS_YES);
        List<ClassroomUser> classroomUserList = classroomUserMapper.selectByClassroomId(classroomChatVo);
        if(classroomUserList != null && classroomUserList.size() > 0){
            log.info("======================在线人数：" + classroomUserList.size());
            for (ClassroomUser classroomUser : classroomUserList) {
                log.info("======================人员：" + classroomUser.toString());
            }
        }
        //发送
        socketIOServer.getBroadcastOperations().sendEvent("onlineUser" + classroomChatVo.getClassroomId().toString(), classroomUserList);

    }
}
